package com.yugao.fintech.antelope.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.system.assembly.SysArticleGroupAssembly;
import com.yugao.fintech.antelope.system.mapper.SysArticleGroupMapper;
import com.yugao.fintech.antelope.system.model.entity.SysArticleGroup;
import com.yugao.fintech.antelope.system.model.rqrs.article.ArticleGroupListReq;
import com.yugao.fintech.antelope.system.model.rqrs.article.ArticleGroupResp;
import com.yugao.fintech.antelope.system.model.rqrs.article.ArticleGroupSaveReq;
import com.yugao.fintech.antelope.system.model.rqrs.article.ArticleGroupUpdateReq;
import com.yugao.fintech.antelope.system.service.SysArticleGroupService;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文章组
 */
@Slf4j
@Service
public class SysArticleGroupServiceImpl extends ServiceImpl<SysArticleGroupMapper, SysArticleGroup> implements IService<SysArticleGroup>, SysArticleGroupService {
    @Autowired
    private SysArticleGroupAssembly articleGroupAssembly;

    @Override
    public List<ArticleGroupResp> listGroup(ArticleGroupListReq req) {
        LambdaQueryWrapper<SysArticleGroup> lqw = SysArticleGroup.lqw()
                .like(StringUtils.isNotEmpty(req.getName()), SysArticleGroup::getName, req.getName())
                .orderByAsc(SysArticleGroup::getSortNo)
                .orderByDesc(SysArticleGroup::getCreatedAt);
        return this.list(lqw).stream()
                .map(articleGroupAssembly::toArticleGroupResp).collect(Collectors.toList());
    }

    @Override
    public ArticleGroupResp getGroup(Long id) {
        SysArticleGroup entity = this.getById(id);
        return articleGroupAssembly.toArticleGroupResp(entity);
    }

    @Override
    public boolean saveGroup(ArticleGroupSaveReq req) {
        SysArticleGroup entity = articleGroupAssembly.toArticleGroup(req);
        return this.save(entity);
    }

    @Override
    public boolean updateGroup(ArticleGroupUpdateReq req) {
        SysArticleGroup entity = articleGroupAssembly.toArticleGroup(req);
        return this.updateById(entity);
    }

    @Override
    public boolean deleteGroup(Long id) {
        SysArticleGroup group = this.getById(id);
        if (Objects.nonNull(group) && group.getArticleNum() > 0) {
            throw new BizException("已有文章引用当前组");
        }
        return this.removeById(id);
    }

    @Override
    public boolean batchDeleteGroup(List<Long> ids) {
        boolean ret = listByIds(ids).stream().anyMatch(e -> e.getArticleNum() > 0);
        if (ret) {
            throw new BizException("已有文章引用当前组");
        }
        return this.removeByIds(ids);
    }
}
